package com.jni.zing;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private EditText ePlusOne;
    private EditText ePlusTwo;
    private TextView tSum;
    private Button bSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        ePlusOne = findViewById(R.id.plus_one);
        ePlusTwo = findViewById(R.id.plus_two);
        tSum = findViewById(R.id.sum);
        bSubmit = findViewById(R.id.sumbmit);
        ePlusOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                Toast.makeText(MainActivity.this, "beforeTextChanged " + s + " start " + start +
//                        " count " + count + " after " + after, Toast.LENGTH_LONG).show();
                Log.e("TextWatcher","beforeTextChanged " + s + " start " + start +
                        " count " + count + " after " + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                Toast.makeText(MainActivity.this, "onTextChanged " + s + " start " + start +
//                        " before " + before + " count " + count, Toast.LENGTH_SHORT).show();
                Log.e("TextWatcher","onTextChanged " + s + " start " + start +
                        " before " + before + " count " + count);
                if(!"".contentEquals(s)){
                    long l = Long.valueOf(s.toString());
                    if (l > Integer.MAX_VALUE) {
                        Toast.makeText(MainActivity.this, "输入的数字太大", Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
//                Toast.makeText(MainActivity.this, "afterTextChanged", Toast.LENGTH_SHORT).show();
            }
        });
        bSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add();
            }
        });
    }

    public void add() {
        String strOne = ePlusOne.getText().toString();
        String strTwo = ePlusTwo.getText().toString();
        if ("".equals(strOne)) {
            Toast.makeText(this, "请输入加数", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(strTwo)) {
            Toast.makeText(this, "请输入被加数", Toast.LENGTH_SHORT).show();
            return;
        }
        int a = Integer.valueOf(strOne);
        int b = Integer.valueOf(strTwo);
        tSum.setText(String.valueOf(addFromJNI(a, b)));
    }

    public native int addFromJNI(int a, int b);


    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
